/**
 * @file STL_RtspResponse.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2021-02-19
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2021
 * 
 */

#include "STL_RtspResponse.h"
#include "STL_Text.h"

namespace STL
{
    STL_RtspResponse::STL_RtspResponse()
    {
        m_version = "RTSP/1.0";
        m_code = 200;
        m_phrase = "OK";
        m_headers["Server"] = "STLHttpServer/0.0.1";
        m_headers["Content-Type"] = "text/plain";
        m_headers["Access-Control-Allow-Origin"] = "*";
        m_headers["Connection"] = "close";
        m_body.clear();
    }

    void STL_RtspResponse::setCode(int statusCode, std::string phrase /*= ""*/)
    {
        m_code = statusCode;
        if (phrase.empty())
        {
            m_phrase = "OK"; //FIXME STL_HttpTools::getHttpCodePhrase(statusCode);
        }
        else
        {
            m_phrase = phrase;
        }
    }
    void STL_RtspResponse::setBody(std::string body)
    {
        m_body = body;
        // m_headers["Content-Length"] = std::to_string(m_body.size());
    }
    void STL_RtspResponse::setBodyFromJson(JSONCPP::Value body)
    {
        m_body = getFastString(body);
        m_headers["Content-Type"] = "application/json";
        // m_headers["Content-Length"] = std::to_string(m_body.size());
    }

    void STL_RtspResponse::setContentType(std::string type)
    {
        if (type.empty())
            return;
        // TODO 检验是否合法
        m_headers["Content-Type"] = type;
    }
    void STL_RtspResponse::setBodyFromFile(std::string filePath)
    {
        //TODO 文件发送时,需要边读取边发送
    }

    std::string STL_RtspResponse::getheader(std::string header)
    {
        if (m_headers.count(header) == 1)
        {
            return m_headers.at(header);
        }
        return "";
    }
    std::string STL_RtspResponse::getWWWAuthorization()
    {
        return getheader("WWW-Authenticate");
    }

    std::string STL_RtspResponse::getWWWAuthorizationType()
    {
        std::string wwwaudh = getheader("WWW-Authenticate");
        if (STL_Text::string_start_with(wwwaudh, "Digest "))
            return "Digest";
        if (STL_Text::string_start_with(wwwaudh, "Basic "))
            return "Basic";
        return "";
    }

    std::string STL_RtspResponse::getWWWAuthorizationValue()
    {
        std::string wwwaudh = getheader("WWW-Authenticate");
        if (STL_Text::string_start_with(wwwaudh, "Digest "))
            return wwwaudh.substr(7);
        if (STL_Text::string_start_with(wwwaudh, "Basic "))
            return wwwaudh.substr(6);
        return "";
    }

    std::map<std::string, std::string> STL_RtspResponse::getWWWAuthorizationMap()
    {
        std::map<std::string, std::string> ret;
        std::string value = getWWWAuthorizationValue();
        std::vector<std::string> valueList = STL_Text::split(value, ",");
        for (int i = 0; i < valueList.size(); i++)
        {
            std::vector<std::string> key_value = STL_Text::split(valueList.at(i), "=");
            if (key_value.size() == 2)
            {
                std::string key = STL_Text::trim(STL_Text::trim(key_value.at(0), " "), "\"");
                std::string value = STL_Text::trim(STL_Text::trim(key_value.at(1), " "), "\"");
                ret[key] = value;
            }
            else
            {
                stlog_warn("parseError: %s \n", valueList.at(i).c_str());
            }
        }
        return ret;
    }

    std::string STL_RtspResponse::getWWWAuthorizationMapValue(std::string key)
    {
        std::string ret = "";
        auto map = getWWWAuthorizationMap();
        if (map.count(key) == 1)
        {
            ret = map.at(key);
        }
        return ret;
    }

    std::string STL_RtspResponse::genHttpMessage()
    {
        std::string ret = "";
        ret = ret + m_version + " " + std::to_string(m_code) + " " + m_phrase + "\r\n";
        if (m_body.empty())
        {
            m_headers.erase("Content-Type");
        }

        m_headers["Content-Length"] = std::to_string(m_body.size());

        for (std::map<std::string, std::string>::iterator it = m_headers.begin(); it != m_headers.end(); it++)
        {
            ret = ret + it->first + ": " + it->second + "\r\n";
        }
        ret = ret + "\r\n" + m_body;
        return ret;
    }

}